﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Windows.Forms;
using System.Drawing;

using xColor = Microsoft.Xna.Framework.Color;
using sColor = System.Drawing.Color;

namespace LevelEditor
{
    public static class Globals
    {
        public static Texture2D Texture2DFromBitmap(Bitmap bitmap)
        {
            xColor[] data = new xColor[bitmap.Width * bitmap.Height];
            for (int y = 0; y < bitmap.Height; y++)
                for (int x = 0; x < bitmap.Width; x++)
                {
                    sColor color = bitmap.GetPixel(x, y);
                    data[x + y * bitmap.Width].R = color.R;
                    data[x + y * bitmap.Width].G = color.G;
                    data[x + y * bitmap.Width].B = color.B;
                    data[x + y * bitmap.Width].A = color.A;
                }

            Texture2D outval = new Texture2D(MainMenu.instance.graphics.device, bitmap.Width, bitmap.Height);
            outval.SetData(data);

            return outval;
        }

        public static Bitmap BitmapFromTexture2D(Texture2D texture)
        {
            xColor[] data = new xColor[texture.Width * texture.Height];
            texture.GetData(data);

            Bitmap outval = new Bitmap(texture.Width, texture.Height);

            for (int y = 0; y < texture.Height; y++)
            {
                for (int x = 0; x < texture.Width; x++)
                {
                    xColor xc = data[x + y * texture.Width];
                    sColor c = sColor.FromArgb(xc.A, xc.R, xc.G, xc.B);
                    outval.SetPixel(x, y, c);
                }
            }

            return outval;
        }
    }
}